<?php
/**
 * Create by PhpStorm
 * User: mozongyou
 * Date: 2021/8/28
 * Time: 4:49 下午
 */

/**
 * 发布者/房间
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 * @author mozongyou
 * @date 2021-08-28 4:49 下午
 */

namespace App\Service\WebSocket;

class PublisherService
{
    use PushService;

    /**
     * 超时时间/秒
     */
    const TIME_OUT = 300;

    /**
     * 订阅
     * @param $fd
     * @param $params
     * @param $api
     * @param $requestId
     * @param $token
     * @author mozongyou
     * @date 2021-08-27 10:00 下午
     */
    public function subscribe($fd, $data)
    {
        $api = $data['api'];
        $requestId = $data['request_id'];
        $params = $data['params'];
        FdService::getInstance()->bindRoom($fd, $params['publisher_id']);
        $this->sender->push($fd, $this->json($api, $requestId));
    }

    /**
     * 取消订阅/退出房间
     * @param $fd
     * @param $params
     * @param $api
     * @param $requestId
     * @param $token
     * @author mozongyou
     * @date 2021-08-27 10:11 下午
     */
    public function unsubscribe($fd, $data)
    {
        $api = $data['api'];
        $requestId = $data['request_id'];
        $roomId = $data['params']['publisher_id'];
        FdService::getInstance()->clearRoomFd($fd, $roomId);
        $this->sender->push($fd, $this->json($api, $requestId));
    }

    /**
     * 断开链接
     * @param $fd
     * @author mozongyou
     * @date 2021-09-26 10:19 上午
     */
    public function close($fd)
    {
        $this->sender->disconnect($fd);
    }

    /**
     * 前端定时请求
     * @param $fd
     * @param $data
     * @author mozongyou
     * @date 2021-09-25 5:26 下午
     */
    public function heartbeat($fd, $data)
    {
        $api = $data['api'];
        $requestId = $data['request_id'];
        $this->sender->push($fd, $this->json($api, $requestId));
    }
}